package com.rongc.client.freight.business.carrier.view.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.business.carrier.model.ConveyanceBean;
import com.rongc.client.freight.business.carrier.request.api.ConveyanceList2MatchApi;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.view.fragment.SupplyConveyanceFragment;
import com.rongc.client.freight.business.waybill.request.api.WaybillTrucksCreateApi;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;

/**
 * Created by Bruce on 2017/7/7.
 */
public class ConveyanceList2MatchActivity extends BaseActivity {
    SupplyConveyanceFragment fragment;
    String supplyId;
    String price;
    String waybillId;
    SupplyBean supplyBean;
    int page=1;
    NormalDialog tipdialog;

    public void doSearch(int page){
        this.page = page;
        ConveyanceList2MatchApi p = new ConveyanceList2MatchApi(new ConveyanceList2MatchApi.ConveyanceFindParams(),
                respWorkersListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void callbackFun1(Bundle arg) {
        page = arg.getInt("page",page);
        doSearch(page);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_match, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_create:
                Bundle bundle = new Bundle();
                bundle.putString("supplyId",supplyId);
                bundle.putString("price",price);
                bundle.putSerializable("supply",supplyBean);
                ActivityUtils.startActivity(ConveyanceList2MatchActivity.this, ConveyanceCreateActivity.class,bundle);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    Response.Listener<JSONObject> respCreateListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                waybillId = response.optString("id");
                tipdialog.show();
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceList2MatchActivity.this);
            }
        }
    };

    Response.Listener<ConveyanceBean[]> respWorkersListener = new Response.Listener<ConveyanceBean[]>() {

        @Override
        public void onResponse(ConveyanceBean[] response) {
            if (HandleCode.requestSuccess()) {
//                ActivityUtils.toast(HandleCode.headerMessage);
                List<ConveyanceBean> datas = new ArrayList<>();
                if (response != null)
                    for (int i = 0;i<response.length;i++){
                        datas.add(response[i]);
                    }
                fragment.setDatas(datas,page);
            } else {
                UtilMethod.dismissProgressDialog(ConveyanceList2MatchActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ConveyanceList2MatchActivity.this);
        }
    };

    @Override
    public void initParams() {

        supplyId = getIntent().getStringExtra("supplyId");
        price = getIntent().getStringExtra("price");
        supplyBean = (SupplyBean) getIntent().getSerializableExtra("supply");
    }


    @Override
    public void initView() {
        initToolbar("我的车源");
        fragment = SupplyConveyanceFragment.getInstance();
        fragment.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        fragment.setListener(new RecyclerBaseAdapter.OnItemClickListener<ConveyanceBean>() {
            @Override
            public void onItemClick(View view, int position, ConveyanceBean data) {

                WaybillTrucksCreateApi p = new WaybillTrucksCreateApi(new WaybillTrucksCreateApi.WaybillTrucksCreateParams(
                        data.getId(),supplyId,price),
                        respCreateListener, errorListener);
                RequestManager.getInstance().call(p);
            }
        });

        ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),fragment,R.id.contentFrame);
        doSearch(1);
        makeTipDialog();
    }

    void makeTipDialog(){
        tipdialog = new NormalDialog(this);
        tipdialog.setCanceledOnTouchOutside(false);
        tipdialog.title("提示")
                .content("请耐心等待货主支付运费。")
                .style(STYLE_TWO)
                .btnNum(1)
                .btnText(new String[]{"确定"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {

                        Bundle bundle = new Bundle();
                        bundle.putString("waybillId",waybillId);
                        ActivityUtils.startActivity(ConveyanceList2MatchActivity.this,ConveyanceWaybillDetailActivity.class,bundle);

                        finish();
                        tipdialog.cancel();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_supply_pipei;
    }
}
